package com.pkt.item.controller;

import com.item.piketao.dao.SpecGroup;
import com.item.piketao.dao.SpecParam;
import com.pkt.item.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author: xinglongge
 * @Date: 2020/3/4 15 49
 * @Description:
 */
@Controller
@RequestMapping("spec")
public class SpecificationController {

    private final SpecificationService specificationService;

    @Autowired
    public SpecificationController(SpecificationService specificationService) {
        this.specificationService = specificationService;
    }

    @GetMapping("groups/{cid}")
    public ResponseEntity<List<SpecGroup>> queryGroupsByCid(@PathVariable("cid") Long cid) {

        List<SpecGroup> specGroups = this.specificationService.queryGroupsByCid(cid);
        if (CollectionUtils.isEmpty(specGroups)){
            return ResponseEntity.noContent().build();
        }
        return ResponseEntity.ok(specGroups);
    }

    /**
     * 根据条件查询规格参数
     * @param gid
     * @return
     */
    @GetMapping("params")
    public ResponseEntity<List<SpecParam>> queryParams(
            @RequestParam(value = "gid",required = false) Long gid,
            @RequestParam(value = "cid",required = false) Long cid,
            @RequestParam(value = "generic",required = false) Boolean generic,
            @RequestParam(value = "search",required = false) Boolean search){
        List<SpecParam> specParams = specificationService.queryParams(gid,cid,generic,search);
        if (CollectionUtils.isEmpty(specParams)){
            return ResponseEntity.noContent().build();
        }
        return ResponseEntity.ok(specParams);
    }
}
